using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSRI.HelperClasses;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Xml;
using System.IO;
using Instedd.Labs.Geo.Library.Kml;
using Instedd.Labs.Geo.Library.Kml.Styles;
using KmlPath = Instedd.Labs.Geo.Library.Kml.Path;

namespace Instedd.Labs.Geo.Library.Tests.Managers
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class KmlWriterFixture
    {
        public KmlWriterFixture()
        {}

        [TestMethod]
        public void KmlWithPlacemarksAndPathsSavesTheKmlFile()
        {
            KmlWriter kmlWriter = new KmlWriter();

            KmlFormat kml = new KmlFormat();
            Style style = new Style("styleid");
            style.LineStyle = new LineStyle();
            kml.Styles.Add(style);
            kml.Folders.Add(new Folder("Tom"));

            kml.Folders[0].Placemarks.Add(new Placemark("I'm at Kirkland", "Tom", new LatLong(47.68166999999999, -122.2075), DateTime.Now));
            kml.Folders[0].Placemarks.Add(new Placemark("I'm at Redmond", "Tom", new LatLong(47.67861000000001, -122.13099), DateTime.Now));

            KmlPath path = new KmlPath("Tom");
            path.Coordinates.Add(new LatLong(47.68166999999999, -122.2075));
            path.Coordinates.Add(new LatLong(47.67861000000001, -122.13099));
            path.Style = style;
            kml.Folders[0].Paths.Add(path);

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;

            using(XmlWriter writer = XmlWriter.Create("test-generated-map.kml", settings))
            {
               kmlWriter.WriteKml(writer, kml);
            }

            StringBuilder kmlContent = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(kmlContent, settings))
            {
                kmlWriter.WriteKml(writer, kml);
            }

            Assert.IsTrue(File.Exists("test-generated-map.kml"));
            Assert.AreEqual(kmlContent.ToString(), File.ReadAllText("test-generated-map.kml"));
        }
    }
}